﻿/*
 * 作者: 谢中涞,博客园:http://www.cnblogs.com/xie-zhonglai
 * 欢迎转载,但请保留出处
 * */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.Reflection;

namespace Liya.Crm.Core
{
    /// <summary>
    /// IQueryable 扩展
    /// </summary>
    public static class IQueryableEx
    {
        /// <summary>
        /// 筛选数据
        /// </summary>
        /// <typeparam name="T">获取实体类型</typeparam>
        /// <param name="source">原表达式</param>
        /// <param name="expression">条件表达式</param>
        /// <returns></returns>
        public static IQueryable<T> Get<T>(this IQueryable<T> source, Expression<Func<T, bool>> expression) where T : class
        {
            if (expression != null)
                source = source.Where(expression);
            return source;
        }

        /// <summary>
        /// 分页筛选数据
        /// </summary>
        /// <typeparam name="T">获取实体类型</typeparam>
        /// <param name="source">原表达式</param>
        /// <param name="expression">条件表达式</param>
        /// <param name="orderPropertyName">实体中排序属性名</param>
        /// <param name="isAscSort">是否是升序排序,false为降序排序</param>
        /// <param name="pgIndex">显示第几页</param>
        /// <param name="pgSize">每页数据量大小,当为小于或等于0的时候,不分页,且并不计算total值</param>
        /// <param name="total">符合条件表达式是记录总数</param>
        /// <returns></returns>
        public static IQueryable<T> Get<T>(this IQueryable<T> source, Expression<Func<T, bool>> expression,
             string orderPropertyName, bool isAscSort, int pgIndex, int pgSize, out int total) where T : class
        {
            source = source.Where(expression).OrderBy(orderPropertyName, isAscSort);
            total = 0;
            if (pgSize > 0)
            {
                total = source.Count();
                source = source.Skip(pgIndex * pgSize).Take(pgSize);
            }
            return source;
        }

        /// <summary>
        /// 根据指定条件筛选数据
        /// </summary>
        /// <typeparam name="T">获取实体类型</typeparam>
        /// <param name="source">原表达式</param> 
        /// <param name="queryParam">查询条件</param>
        /// <param name="total">符合条件的总记录数,该返回值在参数IsPagingQuery为true值时才有效</param>
        /// <returns></returns>
        public static IQueryable<T> Get<T>(this IQueryable<T> source, EFQueryParam<T> queryParam, out int total) where T : class
        {
            total = 0;
            if (queryParam == null)
                return source;

            if (queryParam.Expression != null)
                source = source.Where(queryParam.Expression);

            if (!string.IsNullOrEmpty(queryParam.OrderPropertyName))
                source = source.OrderBy(queryParam.OrderPropertyName, queryParam.IsAscSort);

            //分页查询
            if (queryParam.IsPagingQuery)
            {
                total = source.Count();      //记录总数
                source = source.Skip(queryParam.PageIndex * queryParam.PageSize).Take(queryParam.PageSize);
            }
            return source;
        }

    }
}
